<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>自定义多级导航菜单查询</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    body{
        background: #f5f5f5;
    }
    ul,li{
        list-style: none;
    }

    .clearFix:after{
        content: '';
        display: block;
        clear: both;
    }
    div::-webkit-scrollbar{
        width:10px;
        height:10px;
        /**/
    }
    div::-webkit-scrollbar-track{
        background: rgb(239, 239, 239);
        border-radius:2px;
    }
    div::-webkit-scrollbar-thumb{
        background: #bfbfbf;
        border-radius:10px;
    }
    div::-webkit-scrollbar-thumb:hover{
        background: #333;
    }
    div::-webkit-scrollbar-corner{
        background: #179a16;
    }

    span{
        vertical-align: middle;
    }
    .boxSizing{
        box-sizing: border-box;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    .customBox{
        width: 950px;
        height: 610px;
        margin:50px auto 0;
        background: #fff;
    }
    .customBox > div{
        height: 100%;
    }
    .leftCustom{
        float: left;
        width: 200px;
        overflow-x: hidden;
        overflow-y: auto;
    }
    .leftCustom .navBox{
        width: 100%;
        padding: 4px 0;
        position: relative;
    }
    .leftCustom .navBox.select:before{
        content: '';
        position: absolute;
        width: 3px;
        height: 90%;
        background: #2887e6;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
    }
    .leftCustom .navBox + .navBox:after{
        content: '';
        position: absolute;
        top: 0;
        left: 50%;
        width: 85%;
        transform: translateX(-50%);
        border-top: 1px solid #e9eaeb;
    }
    .leftCustom .navBox p{
        color:#333;
        font-size: 15px;
        cursor: pointer;
        line-height: 22px;
        box-sizing: border-box;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    .leftCustom .navBox p+p{
        margin-top: 4px;
    }
    .leftCustom .navBox p+p:hover{
        background: #f5f7f8;
    }
    .leftCustom .navBox p.oneTitle{
        color:#333;
        font-weight: bold;
    }
    .leftCustom .navBox p.twoTitle{
        color:#333;
    }
    .leftCustom .navBox p.threeTitle{
        color:#333;
    }
    .leftCustom .navBox p.on{
        color:#2887e6;
        font-weight: bold;
        background: #f5f7f8;
    }
    .midCustom{
        float: left;
        width: calc(100% - 450px);
        border: 1px solid #e9eaeb;
        /*overflow: hidden;*/
    }
    .midCustom .searchWrap{
        width: 100%;
        height: 52px;
        padding: 12px;
        border-bottom: 1px solid #e9eaeb;
    }
    .midCustom .searchWrap .searchInput{
        width: 100%;
        height: 100%;
        position: relative;
    }
    .midCustom .searchWrap .inputText{
        width: 100%;
        font-size: 12px;
        font-family: Arial, sans-serif;
        line-height: 16px;
        height: 26px;
        text-align: left;
        padding-left: 20px;
        border: 1px solid #e9eaeb;
    }
    .midCustom .searchWrap .searchInput .searchImg{
        position: absolute;
        width: 14px;
        height: 14px;
        top: 50%;
        left: 3px;
        transform: translateY(-50%);
    }
    .midCustom  .dataList{
        width: 100%;
        height: calc(100% - 52px);
        overflow-x: hidden;
        overflow-y: auto;
    }
    .midCustom .checkectBox{
        display: inline-block;
        width: 12px;
        height: 12px;
        border:1px solid #e9eaeb;
        vertical-align: middle;
        cursor: pointer;
        background: #fff;
        margin-right: 8px;
    }
    .midCustom .checkectBox.tick{
        background: url("./images/tick.png");
        background-size: cover;
    }
    .midCustom .areaBox{
        margin-top: 5px;
    }
    .midCustom .title{
        cursor: pointer;
        line-height: 25px;
    }
    .midCustom .title.disabled .checkectBox{
        background: #eee;
    }
    .midCustom .title.disabled .checkectBox.tick{
        background:#eee url("./images/tick.png");
        background-size: cover;
    }
    .midCustom .title .checkectBox{
        display: inline-block;
        width: 14px;
        height: 14px;
        vertical-align: middle;
    }
    .midCustom .title span{
        display: inline-block;
        max-width: 80%;
        vertical-align: middle;
        font-size: 12px;
        text-align: left;
        text-indent: 0;
    }
    .midCustom .title:hover{
        background: #f5f7f8;
    }
    .midCustom .title.type{
        background: #f5f7f8;
    }
    .rightCustom{
        float: left;
        width: 250px;
        background: #e9eaeb;
        padding: 20px;
        overflow-x: hidden;
        overflow-y: auto;
    }
    .rightCustom .rightTitle{
        font-size: 12px;
        font-weight: bold;
        color:#8D949E;
        line-height: 16px;
        margin-bottom: 12px;
    }
    .rightCustom .rightTitle span.num{
        color:#2887e6;
    }
    .rightCustom .listTitle{
        text-indent: 24px;
        line-height: 34px;
        width: 100%;
        border-radius: 5px;
        background: #fff;
        font-size: 14px;
        color:#1C1E21;
    }
    .rightCustom .selectList li{
        margin-top: 5px;
        width: 100%;
        color:#1C1E21;
        border-radius: 5px;
        background: #fff;
        font-size: 14px;
        padding: 8px 4px;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        cursor: move;
    }
    .rightCustom .selectList li span{
        width: 80%;
    }
    .rightCustom .selectList li .bit{
        display: inline-block;
        width: 15px;
        height: 15px;
        background: url("./images/bit.png");
        background-size: cover;
        vertical-align: middle;
        margin-right: 5px;
    }
    .rightCustom .selectList li .close{
        width: 18px;
        height: 18px;
        background: url("./images/close.png");
        background-size: cover;
        cursor: pointer;
    }
    .emptyData{
        color:#8D949E;
        font-size: 12px;
        line-height: 16px;
        font-weight: bold;
        padding: 12px;
    }
</style>
<body>
    <div class="customBox boxSizing">
        <div class="leftCustom boxSizing">
            <div class="navBox boxSizing">
<!--                <p class="oneTitle">参与互动</p>-->
<!--                <p class="twoTitle">主页帖子</p>-->
<!--                <p class="threeTitle">发消息</p>-->
<!--                <p>多媒体素材</p>-->
<!--                <p>点击量</p>-->
<!--                <p>知名度</p>-->
            </div>
        </div>
        <div class="midCustom boxSizing">
            <div class="searchWrap boxSizing">
                <div class="searchInput">
                    <input type="text" class="inputText boxSizing" placeholder="搜索" />
                    <img src="./images/search.png" class="searchImg" alt="">
                </div>
            </div>
            <div class="dataList">
                <p class="emptyData">找不到结果。</p>
                <div class="areaWrap">
<!--                    <p class="title">-->
<!--                        <i class="checkectBox tick"></i>-->
<!--                        <span>参与活动</span>-->
<!--                    </p>-->
                </div>
            </div>
        </div>
        <div class="rightCustom boxSizing">
            <p class="rightTitle">
               <span>已选择</span>
                <span class="num">21</span>
                <span>栏</span>
            </p>
            <p class="listTitle">广告系列名称</p>
            <ul class="selectList" id="select">
<!--                <li class="boxSizing">-->
<!--                    <i class="bit"></i>-->
<!--                    <span>主语互动</span>-->
<!--                    <i class="close"></i>-->
<!--                </li>-->
            </ul>
        </div>
    </div>
</body>
<script src="js/data.js"></script>
<script src="js/Sortable.min.js"></script>
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script>
    // 赋值左侧和中间列表
    var setDataList = getData.data
    var selectList = []  // 选中数组

    $('.emptyData').hide()

    // 数据初始化渲染
    initRender(setDataList,0)

    // 渲染右侧数据
    renderSelectList()

    // 渲染拖拽事件
    var select = document.getElementById('select')
    Sortable.create(select, {
        animation: 150,
        onUpdate: function (){
            let updateArr = []
            Array.from($('#select li')).forEach(item=>{
                updateArr.push({
                    id:$(item).find('.close').attr('attr-id'),
                    title:$(item).find('span').text()
                })
            })
            selectList = updateArr
        }
    });

    // 点击左侧，中间跳转对应的位置
    $('.leftCustom').on('click','.navBox p',function () {
        if($('.inputText ').val())return alert('搜索中无法触发')

        var id = $(this).attr('attr-id')

        // 左侧点击变色
        $('.leftCustom .navBox p').removeClass('on')
        $(this).addClass('on')

        // 中间滑到相应的位置
        var $oLi = $('.midCustom .areaWrap p[attr-id='+ id +']')
        var top = $('.dataList').scrollTop() + $oLi.offset().top - $('.dataList').offset().top
        $('.dataList').animate({scrollTop: top}, 150);
    })

    // 点击中间，选中或者取消，关联下级
    $('.areaWrap').on('click','p',function () {
        var attributeType = $(this).attr('attributeType')
        if(attributeType == '1'){
            return false
        }
        var isfield = $(this).attr('attr-isfield') // 是否是值
        var isfieldState = isfield == 'true'

        // 获取选中ele，判断是否选中
        var $checkectBox = $(this).find('.checkectBox')
        var isSelect = $checkectBox.hasClass('tick')
        var id = $(this).attr('attr-id') // 获取父级id
        var parentId = $(this).attr('parent-id') // 上级id

        // 当前级状态修改
        if(!isSelect){
            // 值则push
            if(isfieldState){
                selectList.push({
                    title:$(this).find('span').text(),
                    id:id,
                })
                selectList = unique(selectList)
            }
        }else{
            // 值则删除
            if(isfieldState){
                var index = selectList.findIndex(v=>v.id == id)
                selectList.splice(index,1)
            }
        }

        $checkectBox.toggleClass('tick')
        // 下级关联状态取消
        checkLeve(id,!isSelect)

        // 渲染右侧列表
        renderSelectList()

        // 勾选关联上级
        enoughCheck(parentId)
    })

    // 右侧删除
    $('.rightCustom').on('click','.close',function () {
        var id = $(this).attr('attr-id')
        var $oLi = $(this).parent()
        var index = $oLi.index()

        // 中间列表选中取消
        var $midOLi = $('.midCustom .areaWrap p[attr-id='+ id+']' )
        var parentId = $midOLi.attr('parent-id') // 上级id
        $midOLi.find('.checkectBox').removeClass('tick')

        // 选中数据删除
        selectList.splice(index,1)
        $oLi.remove()
        $('.num').text(selectList.length)

        // 勾选关联上级
        enoughCheck(parentId)
    })

    // 搜索栏
    $('.inputText').on('keyup',function () {
        var value = $(this).val()

        if(!value){
            $('.midCustom .areaWrap p').show()
            $('.emptyData').hide()
        }else{
            var searchList = []
            var searchReg = new RegExp(value);
            var $title = $('.midCustom .areaWrap p')
            if($title.length>0){
                Array.from($title).forEach(item=>{
                    // 非值隐藏
                    var isfield = $(item).attr('attr-isfield')
                    var text = $(item).find('span').text()

                    if(isfield){
                        // 值
                        if(searchReg.test(text)){
                            searchList.push(text)
                            $(item).show()
                        }else{
                            $(item).hide()
                        }
                    }else{
                        // 非值隐藏
                        $(item).hide()
                    }
                })
            }
            $('.emptyData')[searchList.length==0?'show':'hide']()
        }
    })


    /**
     * 初始渲染
     * @param arr 数据数组
     * @param leve 层级
     */
    function initRender(arr,leve,checkect,parentId) {
        var leftStyle = {}  // 子层
        if(leve == 0){
            leftStyle = {
                fontWeight:'bold'
            }
        }
        leve++
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i]
            var isfield = item.isfield  // 是否是值
            var children = item.children  // 子层
            var attributeType = item.attributeType  // 子层

            // 排序
            if(children && children.length>0){
                children = children.sort(function (a,b) { return a.sort - b.sort })
            }
            leftStyle.textIndent = leve * 10+'px'
            var id = item.id || Math.ceil(Date.now() * Math.random())
            // 接收上层选中，无则取默认参数
            var new_checkect = checkect || item.checkect  // 是否选中

            // 添加左侧数据
            var $left_p = $('<p>'+ item.title +'</p>')
                .attr({
                    'attr-id':id,
                })
                .css(leftStyle)
            !isfield && $('.leftCustom .navBox').append($left_p) // 非值才可以添加

            // 中间添加数据
            var $mid_p = $('<p class="'+ ( attributeType=='1'?'disabled':'' ) +'"> <i class="checkectBox '+  (new_checkect?'tick':'') +'"></i> <span>'+ item.title +'</span></p>')
                .addClass([
                    'title',
                    isfield?'':'type'
                ])
                .css('textIndent',leve * 10+'px')
                .attr({
                    'attr-isfield':isfield,
                    'attr-id':id,
                    'parent-id':parentId,
                    'attributeType':attributeType,
                })
            $('.midCustom .areaWrap').append($mid_p)

            // 非值添加数据
            if(isfield && new_checkect && attributeType!='1'){
                selectList.push({
                    title:item.title,
                    id:String(id)
                })
            }

            // 递归
            children && children.length>0 && initRender(children,leve,new_checkect,id)
        }
    }

    /*
    *  关联上下级
    * */
    function checkLeve(id,state) {
        var $p = $('p[parent-id=' + id + ']')

        // 下级删除
        if($p.length>0){
            Array.from($p).forEach(element=>{
                var isfield = $(element).attr('attr-isfield')  // 是否是值
                var childId = $(element).attr('attr-id') // 获取父级id
                var isfieldState = isfield == 'true'
                var attributeType = $(element).attr('attributeType')

                // 判断状态勾选
                if(attributeType!='1'){
                    if(state){
                        // 勾选
                        $(element).find('.checkectBox').addClass('tick')

                        // 值则push
                        if(isfieldState ){
                            selectList.push({
                                title:$(element).find('span').text(),
                                id:childId,
                            })

                            selectList = unique(selectList)
                        }
                    }else{
                        // 取消勾选
                        $(element).find('.checkectBox').removeClass('tick')

                        // 值则删除
                        if(isfieldState){
                            var index = selectList.findIndex(v=>v.id == childId)
                            selectList.splice(index,1)
                        }
                    }
                }


                // 下级状态改变
                checkLeve(childId,state)
            })
        }
    }


    /**
     *  渲染选中数组
     */
    function renderSelectList() {
        $('.rightCustom .selectList').empty()
        $('.num').text(selectList.length)
        selectList.forEach(item=>{
            var $oLi = $('<li class="boxSizing"><i class="bit"></i><span>'+ item.title +'</span><i class="close" attr-id="'+ item.id +'"></i></li>')
            $(".rightCustom .selectList").append($oLi)
        })
    }

    /**
     * 勾满足选中
     * @param parent-id 改变状态的parent-id
     */
    function enoughCheck(parentId) {
        if(!parentId)return false
       var $title = $('.midCustom .areaWrap p[parent-id='+ parentId+']')
       var $select = $title.find('.tick')

       // 判断是否相等给与状态
       var selectState = $title.length == $select.length

       // 赋予上一层状态
       var $parent = $('.midCustom .areaWrap p[attr-id=' + parentId + ']')
       var id = $parent.attr('parent-id')
       $parent.find('.checkectBox')[
           selectState?'addClass':'removeClass'
       ]('tick')

       // 继续寻找上一层
        enoughCheck(id)
    }

    // 数组去重
    function unique(arr){
        for(var i=0; i<arr.length; i++){
            for(var j=i+1; j<arr.length; j++){
                if(arr[i].id==arr[j].id){         //第一个等同于第二个，splice方法删除第二个
                    arr.splice(j,1);
                    j--;
                }
            }
        }
        return arr;
    }

</script>
</html>
